<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/14
 * Time: 14:32
 */
namespace App\Http\Controllers\Manage;

use App\Model\Affix;
use App\Model\Annex;
use Log;
use App\Model\Knowledge;
use App\Model\KnowledgeCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

use App\Model\ProductCategory;
use OSS\OssClient;
use OSS\Core\OssException;

class KnowledgeCategoryController extends Controller {
//    public $point;
//    public $bucket;
//
//    /**
//     * 创建阿里云对象
//     * @return OssClient
//     */
//    public function oss(){
//        $accessKeyId = "LTAIm0cnDLJs1aen";
//        $accessKeySecret = "Pc3M4EN1yPSdU8zny3Ln4QppC58P3G";
//        $endpoint = "http://oss-cn-shanghai.aliyuncs.com";
//        $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
//        $this->point = "oss-cn-shanghai.aliyuncs.com";
//        $this->bucket = "testxiang";
//        return $ossClient;
//    }

    public function knowledgeCategoryList() {
        $knowledgeCategory = KnowledgeCategory::orderBy('rank','desc')->orderBy('create_time','desc')->Paginate(20);
        $count = KnowledgeCategory::count();
        return view('manage.knowledge.knowledgeCategoryList',['knowledgeCategory'=>$knowledgeCategory, 'count' => $count, 'active1' => 12, 'active2' => 3]);
    }

    public function knowledgeCategoryAdd() {
        return view('manage.knowledge.knowledgeCategoryAdd', ['active1' => 12, 'active2' => 4]);
    }

    public function knowledgeCategoryCreate() {
        #设置oss参数
        $ossClient = $this->oss();
        $rank = input::get('rank');
        $name = input::get('name');
        $url = input::get('path');

        if(empty($name)) {
            try {
                $ossClient->deleteObject($this->bucket, $url);
            } catch (OssException $e) {
//                        print $e->getMessage();
            }
            return [
                'code' => 102,
                'msg' => '分类不能为空!'
            ];
        }
        $repetition = KnowledgeCategory::where('name',$name)->first();
        if($repetition) {
            try {
                $ossClient->deleteObject($this->bucket, $url);
            } catch (OssException $e) {
//                        print $e->getMessage();
            }
            return [
                'code' => 103,
                'msg' => '分类已存在!'
            ];
        }
        $map = KnowledgeCategory::insert([
            'name' => $name,
            'rank' => $rank,
            'icon' => $url,
        ]);

        if($map) {
            return [
                'code' => 100,
                'msg' => '添加成功!'
            ];
        }
        else {
            try {
                $ossClient->deleteObject($this->bucket, $url);
            } catch (OssException $e) {
            }
            return [
                'code' => 101,
                'msg' =>'添加失败!'
            ];
        }


    }

    public function knowledgeCategoryEdit($id) {
        $knowledgeCategory_id = $id;
        $knowledgeCategory = KnowledgeCategory::find($knowledgeCategory_id);
        if (!empty($knowledgeCategory)) {
            return view('manage.knowledge.knowledgeCategoryEdit', ['knowledgeCategory' => $knowledgeCategory,'active1' => 12, 'active2' => 3]);
        }
        else {

        }
    }

    public function knowledgeCategoryUpdate() {
        #设置oss参数
        $ossClient = $this->oss();
        $id = input::get('id');
        $data['rank'] = input::get('rank');
        $data['name'] = input::get('name');
        $data['icon'] = input::get('path');
        $before = KnowledgeCategory::where('id',$id)->first();

        if(empty($data['name'])) {
            try {
                $ossClient->deleteObject($this->bucket, $data['icon']);
            } catch (OssException $e) {
//                        print $e->getMessage();
            }
            return [
                'code' => 102,
                'msg' => '缺少元素!'
            ];
        }
        $update = KnowledgeCategory::where('id',$id)->update($data);
        if($update) {
            try {
                $ossClient->deleteObject($this->bucket, $before->icon);
            } catch (OssException $e) {
//                        print $e->getMessage();
            }
            return [
                'code' => 100,
                'msg' => '修改成功!'
            ];
        }
        else {
            try {
                $ossClient->deleteObject($this->bucket,$data['icon']);
            } catch (OssException $e) {
//                        print $e->getMessage();
            }
            return [
                'code' => 101,
                'msg' =>'修改失败!'
            ];
        }
    }

    public function knowledgeCategoryDelete(){
        #设置oss参数
        $ossClient = $this->oss();
        $id = Input::get('id');
        $object = KnowledgeCategory::find($id)['icon'];
        $delete = KnowledgeCategory::where('id',$id)->delete();
        if($delete){
            try {
                $ossClient->deleteObject($this->bucket,$object);
            } catch (OssException $e) {
//                        print $e->getMessage();
            }
            return [
                'code'=>100,
                'msg'=>'删除成功！'
            ];
        }else{
            return [
                'code'=>101,
                'msg'=>'删除失败！'
            ];
        }
    }

    public function knowledgeCategoryStatus() {
        $id = Input::get('id');
        $status = Input::get('status');

        if($status == 1) {
            $del = KnowledgeCategory::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }else{
            $del = KnowledgeCategory::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }

    private function labelCreate($label) {
        Log::info('label here');
    }
}